package com.hk.activity;

import net.slidingmenu.tools.br.AdSize;
import net.slidingmenu.tools.br.AdView;
import net.slidingmenu.tools.st.SpotManager;

import org.apache.http.message.BasicNameValuePair;
import com.gc.materialdesign.views.LayoutRipple;
import com.hk.R;
import com.hk.db.DBHelper;
import com.hk.util.CustomDialog;
import com.hk.util.Utils;
import com.umeng.analytics.MobclickAgent;
import com.umeng.onlineconfig.OnlineConfigAgent;
import com.umeng.update.UmengUpdateAgent;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

/*
 * 主页
 */
public class MainActivity extends Activity {

	private LayoutRipple mlayPart1, mlayPart2, mlayPart3, mlayPart4, mlayPart5,
			mlayPart6, mlayPart7;
	private TextView mtvPart1, mtvPart2, mtvPart3, mtvPart4, mtvPart5,
			mtvPart6, mtvPart7;
	private RelativeLayout mAdContainer, showAdContainer;
	private Button mbtnCloseAd, mbtnAbout;

	private String mPageName = "MainActivity";
	Context context;
	CustomDialog.Builder builder;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		UmengUpdateAgent.update(this);
		context = this;
		findViewByID();
		initDB();
		setQuestionYet();
		initAD();
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(mPageName);
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(mPageName);
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		builder = null;
	}

	protected void findViewByID() {
		mlayPart1 = (LayoutRipple) this.findViewById(R.id.layPart1);
		mlayPart2 = (LayoutRipple) this.findViewById(R.id.layPart2);
		mlayPart3 = (LayoutRipple) this.findViewById(R.id.layPart3);
		mlayPart4 = (LayoutRipple) this.findViewById(R.id.layPart4);
		mlayPart5 = (LayoutRipple) this.findViewById(R.id.layPart5);
		mlayPart6 = (LayoutRipple) this.findViewById(R.id.layPart6);
		mlayPart7 = (LayoutRipple) this.findViewById(R.id.layPart7);

		mAdContainer = (RelativeLayout) findViewById(R.id.AdContainer);
		showAdContainer = (RelativeLayout) findViewById(R.id.showAdContainer);
		mbtnCloseAd = (Button) this.findViewById(R.id.btnCloseAd);
		mbtnAbout = (Button) this.findViewById(R.id.btnAbout);

		mtvPart1 = (TextView) this.findViewById(R.id.tvPart1);
		mtvPart2 = (TextView) this.findViewById(R.id.tvPart2);
		mtvPart3 = (TextView) this.findViewById(R.id.tvPart3);
		mtvPart4 = (TextView) this.findViewById(R.id.tvPart4);
		mtvPart5 = (TextView) this.findViewById(R.id.tvPart5);
		mtvPart6 = (TextView) this.findViewById(R.id.tvPart6);
		mtvPart7 = (TextView) this.findViewById(R.id.tvPart7);

		mlayPart1.setOnClickListener(listener);
		mlayPart2.setOnClickListener(listener);
		mlayPart3.setOnClickListener(listener);
		mlayPart4.setOnClickListener(listener);
		mlayPart5.setOnClickListener(listener);
		mlayPart6.setOnClickListener(listener);
		mlayPart7.setOnClickListener(listener);

		mbtnCloseAd.setOnClickListener(listener);
		mbtnAbout.setOnClickListener(listener);
	}

	// 设置做题进度
	public void setQuestionYet() {
		for (int i = 1; i <= 7; i++) {
			String val = Utils.getValue(getApplicationContext(), "Question_"
					+ i);
			val = (val != "") ? String.valueOf(Integer.valueOf(val) + 1) : "0";
			switch (i) {
			case 1:
				mtvPart1.setText("已做" + val + "/全部90");
				break;
			case 2:
				mtvPart2.setText("已做" + val + "/全部96");
				break;
			case 3:
				mtvPart3.setText("已做" + val + "/全部14");
				break;
			case 4:
				mtvPart4.setText("已做" + val + "/全16");
				break;
			case 5:
				mtvPart5.setText("已做" + val + "/全部20");
				break;
			case 6:
				mtvPart6.setText("已做" + val + "/全部2");
				break;
			case 7:
				mtvPart7.setText("已做" + val + "/全部13");
				break;
			}
		}
	}

	View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			String part = "";
			switch (v.getId()) {
			case R.id.layPart1:// 交际英语
				part = "1";
				Utils.start_Activity(MainActivity.this,
						Question1Activity.class, new BasicNameValuePair(
								"QuestionType", part), new BasicNameValuePair(
								"Head", "交际英语"));
				break;
			case R.id.layPart2:// 词汇与结构
				part = "2";
				Utils.start_Activity(MainActivity.this,
						Question1Activity.class, new BasicNameValuePair(
								"QuestionType", part), new BasicNameValuePair(
								"Head", "词汇与结构"));
				break;
			case R.id.layPart3:// 阅读理解1
				part = "3";
				Utils.start_Activity(MainActivity.this,
						ListQuestionActivity.class, new BasicNameValuePair(
								"QuestionType", part));
				break;
			case R.id.layPart4:// 完形填空
				part = "4";
				Utils.start_Activity(MainActivity.this,
						Question2Activity.class, new BasicNameValuePair(
								"QuestionType", part), new BasicNameValuePair(
								"Head", "完形填空"));
				break;
			case R.id.layPart5:// 英译汉
				part = "5";
				Utils.start_Activity(MainActivity.this,
						Question2Activity.class, new BasicNameValuePair(
								"QuestionType", part), new BasicNameValuePair(
								"Head", "英译汉"));
				break;
			case R.id.layPart6:// 作文
				part = "6";
				Utils.start_Activity(MainActivity.this,
						Question2Activity.class, new BasicNameValuePair(
								"QuestionType", part), new BasicNameValuePair(
								"Head", "作文"));
				break;
			case R.id.layPart7:// 阅读理解2
				part = "7";
				Utils.start_Activity(MainActivity.this,
						ListQuestionActivity.class, new BasicNameValuePair(
								"QuestionType", part));
				break;

			case R.id.btnCloseAd:
				showAdContainer.setVisibility(View.GONE);
				break;
			case R.id.btnAbout:
				Utils.start_Activity(MainActivity.this, AboutActivity.class,
						new BasicNameValuePair("", ""));
				break;
			}
		}
	};

	// 初始化数据库
	public void initDB() {
		String dbIsLoad = Utils.getValue(getApplicationContext(), "DbIsLoad");
		if (dbIsLoad != null && dbIsLoad.equals("yes")) {
			// 已装载数据库
		} else {
			if (DBHelper.copyDB(getApplicationContext())) {
				Utils.putValue(getApplicationContext(), "DbIsLoad", "yes");
			}
		}
	}

	// 初始化广告位
	public void initAD() {
		String showAd = OnlineConfigAgent.getInstance().getConfigParams(this,
				"ShowInlineAd");
		if (showAd.equals("Yes")) {
			showBanner();
		} else {
			showAdContainer.setVisibility(View.GONE);
		}
	}

	private void showBanner() {

		// 广告条接口调用（适用于应用）
		// 将广告条adView添加到需要展示的layout控件中
		AdView adView = new AdView(context, AdSize.FIT_SCREEN);
		mAdContainer.addView(adView);
	}

	/**
	 * 监听返回--是否退出程序
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		boolean flag = true;
		if (keyCode == KeyEvent.KEYCODE_BACK) {

			builder = new CustomDialog.Builder(this);
			String showAd = OnlineConfigAgent.getInstance().getConfigParams(
					this, "ShowExitAd");
			if (showAd.equals("Yes")) {
				SpotManager.getInstance(this).showSpotAds(this);
				AdView adView = new AdView(context, AdSize.FIT_SCREEN);
				builder.setContentView(adView);
			}
			builder.setTitle("您确定要退出学习吗？");
			builder.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							finish();
						}
					});

			builder.setNegativeButton("取消",
					new android.content.DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});

			builder.create().show();
		} else {
			flag = super.onKeyDown(keyCode, event);
		}
		return flag;
	}
}
